﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pg.BioMedics.SDR.Entities;

namespace Pg.BioMedics.SDR.Services.Algorithms
{
    /// <summary>
    /// Implements binart RSV algorithm for similrity measurement between token sets
    /// </summary>
    internal class RSVBinaryAlgorithm : IRSVAlgorithm
    {
        #region Fields

        private IWeightingAlgorithm weightingAlgorithm = new WeightingBinaryAlgorithm();

        #endregion

        #region IRSVAlgorithm Members

        /// <summary>
        /// Calculates relevance score between query Q and 
        /// document D
        /// </summary>
        /// <param name="Q">Query tokens</param>
        /// <param name="D">Document tokens</param>
        /// <returns>RSV value</returns>
        public double Calculate(Token[] Q, Token[] D)
        {
            double rsv = 0;
            for (int i = 0; i < Q.Length; i++)
                for (int j = 0; j < D.Length; j++)
                    rsv += weightingAlgorithm.CalculateQWeight(Q[i], Q) * weightingAlgorithm.CalculateQWeight(Q[i], Q);

            return rsv;
        }

        #endregion
    }
}
